package com.zhudeshuai.zhudemolist.widget;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by zhudeshuai on 16/2/27.
 */
public class TouchMoveView extends LinearLayout {

    private static final String TAG = "TouchMoveView";
    private final Context mContext;
    private ViewDragHelper mViewHelper;
    private MyCallback mCallback;

    public TouchMoveView(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public TouchMoveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        mCallback = new MyCallback(this);
        this.mViewHelper = ViewDragHelper.create(this, 1.0f, mCallback);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return this.mViewHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.mViewHelper.processTouchEvent(event);
        return true;
    }

    private class MyCallback extends ViewDragHelper.Callback {

        private final TouchMoveView mTouchView;
        private View mChildView;

        public MyCallback(TouchMoveView touchMoveView) {
            this.mTouchView = touchMoveView;
        }

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            this.mChildView = child;
            return true;
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if (left < getPaddingLeft()) {
                return getPaddingLeft();
            }
            if (left > (getWidth() - child.getWidth()))
                return getWidth() - child.getWidth();
            return left;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            if (top < getPaddingTop())
                return getPaddingTop();
            if (top > (getHeight() - child.getHeight())) {
                return getHeight() - child.getHeight();
            }
            return top;
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {

            int left = releasedChild.getLeft();
            if (left <= getWidth() / 2 - releasedChild.getWidth() / 2) {
                //靠左
                if (!mViewHelper.smoothSlideViewTo(releasedChild, getPaddingLeft(), releasedChild.getTop())) {
                    return;
                }
            } else {
                //靠右
                if (!mViewHelper.smoothSlideViewTo(releasedChild, getWidth() - releasedChild.getWidth(), releasedChild.getTop())) {
                    return;
                }
            }
            ViewCompat.postInvalidateOnAnimation(this.mTouchView);
        }

    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mViewHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }
}
